package com.feicheng.iou.service.impl;

import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.PaymentOptions;
import com.feicheng.iou.entity.ProjectService;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.mapper.PaymentOptionsMapper;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.PaymentOptionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.iou.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
public class PaymentOptionsServiceImpl extends ServiceImpl<PaymentOptionsMapper, PaymentOptions> implements PaymentOptionsService {

    @Autowired
    PaymentOptionsMapper paymentOptionsMapper;

    @Override
    public ResultInfoV110 selectPaymentOptionsServiceList() {
        log.info("PaymentOptionsServiceImpl 的selectPaymentOptionsServiceList方法进入");
        List<PaymentOptions> paymentOptionsList = paymentOptionsMapper.selectList(null);
        log.info("PaymentOptionsServiceImpl 的selectPaymentOptionsServiceList方法结束");
        return ResultInfoV110.success(paymentOptionsList);
    }

    @Override
    public ResultInfoV110 selectPaymentOptionsListPage(Integer page, Integer size, PaymentOptions paymentOptions) {
        log.info("PaymentOptionsServiceImpl 的selectPaymentOptionsListPage方法进入" + paymentOptions);

        Integer pageIndex = PageUtil.getPageIndex(page, size);
        List<PaymentOptions> paymentOptionsList = paymentOptionsMapper.queryPaymentOptionList(pageIndex, size, paymentOptions.getName());

        Integer total = paymentOptionsMapper.queryPaymentOptionListCount(paymentOptions.getName());
        Map<String, Integer> pageInfo = PageUtil.getPageInfo(page, size, total);
        HashMap<String, Object> result = new HashMap<>();
        result.put("pageInfo", pageInfo);
        result.put("list", paymentOptionsList);

        log.info("PaymentOptionsServiceImpl 的selectPaymentOptionsListPage方法结束");
        return ResultInfoV110.success(result);
    }

    @Override
    public PaymentOptions selectPaymentOptionsByid(Integer id) {
        log.info("PaymentOptionsServiceImpl 的selectPaymentOptionsByid方法进入");
        log.info("PaymentOptionsServiceImpl 的selectPaymentOptionsByid方法结束");
        return paymentOptionsMapper.selectById(id);
    }

    @Override
    public ResultInfoV110 insert(PaymentOptions paymentOptions) {
        log.info("PaymentOptionsServiceImpl 的insert方法进入");

        int insertPaymentOptionsResult = paymentOptionsMapper.insert(paymentOptions);
        if (insertPaymentOptionsResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加分期付款服务信息"));
        }

        log.info("PaymentOptionsServiceImpl 的insert方法进入");
        return ResultInfoV110.success();
    }

    @Override
    public ResultInfoV110 update(PaymentOptions paymentOptions) {
        log.info("PaymentOptionsServiceImpl 的update方法进入");

        int updatePaymentOptionsResult = paymentOptionsMapper.updateById(paymentOptions);
        if (updatePaymentOptionsResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改分期付款服务信息"));
        }

        log.info("PaymentOptionsServiceImpl 的update方法进入");
        return ResultInfoV110.success();
    }

    @Override
    public ResultInfoV110  delete(Integer paymentOptionId) {
        log.info("PaymentOptionsServiceImpl 的delete方法进入");

        int deletePaymentOptionsResult = paymentOptionsMapper.deleteById(paymentOptionId);
        if (deletePaymentOptionsResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "删除分期付款服务信息"));
        }

        log.info("PaymentOptionsServiceImpl 的delete方法进入");
        return ResultInfoV110.success();
    }

    @Override
    public ResultInfoV110 updateStatus(Integer paymentOptionId, boolean status) {
        log.info("PaymentOptionsServiceImpl 的updateStatus方法进入");

        PaymentOptions paymentOptions = new PaymentOptions();
        paymentOptions.setId(paymentOptionId);
        paymentOptions.setStatus(status);
        int updatePaymentOptionsResult = paymentOptionsMapper.updateById(paymentOptions);
        if (updatePaymentOptionsResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改分期付款服务信息"));
        }

        log.info("PaymentOptionsServiceImpl 的updateStatus方法进入");
        return ResultInfoV110.success();
    }
}
